<?php

namespace App\Admin\Controllers;

use App\Branches;
use App\Chest;
use App\ChestType;
use App\Http\Controllers\Controller;
use App\User;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ChestController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('电柜管理')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {

        return $content
            ->header('电柜管理')
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('电柜管理')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('电柜管理')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Chest);
        $grid->model()->take(100);
        $grid->id('设备编号')->sortable();
        $grid->chest_number('IMEI')->display(function ($value) {
            $route = route('grip.index', ['chest_number' => $value]);
            return '<a href="' . $route . '">' . $value . '</a>';
        });
        $grid->chest_name('电柜名称');
        $grid->branches()->branches_name('网点名称');
        $options = ['0' => '离线', '1' => '在线'];
        $grid->state('状态')->options($options)->display(function ($value) use ($options) {
            $humanStatus = $value ? array_get($options, $value, '离线') : '离线';
            return '<button class="btn btn-sm btn-twitter dropdown-toggle" style="width: 100%">' . $humanStatus . '</button>';
        });
        $grid->signal('信号强度');
        $grid->chest()->type_name('电柜类型')->display(function ($value) {
            $route = route('chestType.index', ['type_name' => $value]);
            return '<a href="' . $route . '">' . $value . '</a>';
        });
        $grid->deposit('电柜押金');
        $grid->agent()->name('生产厂家');
        $grid->inv()->name('投资方');
        $grid->partner()->name('合作厂家');
        $grid->is_battery('是否为电池柜')->using(['0' => '否', '1' => '是']);
        $grid->created_at('添加时间');
        $grid->updated_at('编辑时间');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Chest::findOrFail($id));
        $show->id('设备编号');
        $show->chest_number('设备编码');
        $show->chest_name('电柜名称');
        $show->branches()->branches_name('网点名称');
        $show->chest()->type_name('电柜类型');
        $show->is_battery('是否为电池柜')->using(['0' => '否', '1' => '是']);
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Chest);
        $form->text('chest_number', '电柜编号')->rules("required");
        $form->text('chest_name', '电柜名称')->rules("required");
        $form->text('deposit', '电柜押金');
        $form->select('branches_id', '所属网点')->options(Branches::getSelectOptions());
        $form->select('agent_id', '生产厂家')->options(User::getAgentSelectOptions());
        $form->select('invesor_id', '投资方')->options(User::getInvSelectOptions());
        $form->select('parteny_id', '合作厂家')->options(User::getParSelectOptions());
        $form->select('type_id', '电柜类型')->options(ChestType::getSelectOptions());
        $form->radio('is_battery', '是否为电池柜')->options(['0' => '否', '1'=> '是'])->default('0');
        return $form;
    }
}
